package single.shop.web.freemarker.method;

import java.util.Calendar;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import freemarker.template.TemplateMethodModel;
import freemarker.template.TemplateModelException;

/**
 * 日期时间方法
 * 
 * @author Yinyt
 *
 */
public class DateTimeMethod implements TemplateMethodModel
{

	private static final Logger log = LoggerFactory.getLogger(DateTimeMethod.class);

	@Override
	public Object exec(List args) throws TemplateModelException
	{

		try
		{
			if (args.size() == 0)
			{
				// 返回当前时间
				return System.currentTimeMillis();
			}
			if (args.size() == 1)
			{
				String p = (String) args.get(0);
				if (p.equalsIgnoreCase("todaydate"))
				{
					// 当天的零时
					Calendar cal = Calendar.getInstance();
					cal.set(Calendar.HOUR_OF_DAY, 0);
					cal.set(Calendar.MINUTE, 0);
					cal.set(Calendar.SECOND, 0);
					cal.set(Calendar.MILLISECOND, 0);
					return cal.getTimeInMillis();
				}
			}
		} catch (Exception e)
		{
			log.error("exec exception, return current datetime - ", e);
			return System.currentTimeMillis();
		}
		return System.currentTimeMillis();
	}
}
